<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    .box {
      width: 5rem;
      height: 5rem;
      background-color: orange;
    }
    span {
      font-size: 0.5rem;
    }
  </style>
</head>
<body>
  <div class="box"></div>
  <span>我是文本</span>

  <script>
    const htmlEl = document.documentElement // 获取html元素

    function setRemUnit() { 
      const htmlWidth = htmlEl.clientWidth // 获取 html 宽度
      const htmlFontSize = htmlWidth / 10 //  html 的 font-size 为 html 宽度的1/10
      htmlEl.style.fontSize = htmlFontSize + 'px'
    }
    
    setRemUnit()
    // 监听浏览器窗口大小 
    window.addEventListener('resize', setRemUnit)

  </script>
</body>
</html>
